//
//  XCInTheSingerViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/16.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCInTheSingerViewController.h"
#import "XCInTheSingerTableViewCell.h"
#import "XCAFNetTool.h"
#import "UIImageView+WebCache.h"
#import "XCInTheSingerTableViewCell.h"
#import "XCUrlManger.h"
#import "XCInTheSingerModel.h"
#import "XCUnderSingerViewController.h"
@interface XCInTheSingerViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSString *picUrl;

@property (nonatomic, retain) UIImageView *inTheSingerListImageView;

@property (nonatomic, copy) NSString *navigationName;

@property (nonatomic, retain) NSNumber *singerListId;


@end

@implementation XCInTheSingerViewController

- (instancetype)initWithId:(NSNumber *)singerListId WithTitle:(NSString *)title;
{
    self = [super init];
    if (self) {
        
        self.singerListId = singerListId;
        self.navigationName = title;
        
    }
    return self;
}

- (void)dealloc {
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_dataSource release];
    [_dataSource release];
    [_singerListId release];
    [_picUrl release];
    [_tableView release];
    [_navigationName release];
    [_inTheSingerListImageView release];

    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.navigationName;
    [self inTheSingerNetWork];
    [self inTheSingerTableViewBuild];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
}
- (void)inTheSingerNetWork {
    NSLog(@"%@", self.singerListId);
    [XCAFNetTool GETNetWithUrl:[XCUrlManger inTheSingerNetRequest:self.singerListId] body:nil headerFile:nil response:XCJSON success:^(id result) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in result[@"data"]) {
            XCInTheSingerModel *inTheSingerModel = [[XCInTheSingerModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:inTheSingerModel];
            [inTheSingerModel release];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
}
- (void)inTheSingerTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.inTheSingerListImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    self.inTheSingerListImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Image-%d", arc4random() % 10]];
    [view addSubview:self.inTheSingerListImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCInTheSingerTableViewCell class] forCellReuseIdentifier:@"XCInTheSingerTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [view release];
    [_tableView release];
    [_inTheSingerListImageView release];
}
#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCInTheSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCInTheSingerTableViewCell"];
    XCInTheSingerModel *inTheSingerModel = self.dataSource[indexPath.row];
    cell.inTheSingerModel = inTheSingerModel;
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XCInTheSingerModel *intheSingerModle = self.dataSource[indexPath.row];
    NSNumber *singer_id = intheSingerModle.singer_id;
    XCUnderSingerViewController *underSingerVC = [[XCUnderSingerViewController alloc] initWithSinger_id:singer_id];
    [self.navigationController pushViewController:underSingerVC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.inTheSingerListImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.inTheSingerListImageView.transform = CGAffineTransformMakeScale(scale, scale);
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
